import sys

class Solution:
    def get_group(self, children, mappings):
        in_group = [0] * len(children)
        loop = 0
        while True:
            if all([c != 0 for c in in_group]):
                break
            loop += 1
            c = children[in_group.index(0)]
            in_group[children.index(c)] = 1
            nc = mappings[c]
            while in_group[children.index(nc)] == 0:
                in_group[children.index(nc)] = 1
                nc = mappings[nc]
        return loop


if __name__ == "__main__":
    fp = sys.stdin
    num = int(sys.stdin.readline())
    lines = [fp.readline().split() for i in range(num)]
    children = [l[0] for l in lines]
    mappings = {c: [l[1] for l in lines if l[0] == c][0] for c in children}
    
    s = Solution()
    print(s.get_group(children, mappings))
